ContactUsManager = Class.create({
	
	initialize: function() {
		if ($("contact"))
		{
			this.txtSubject		= $("txtSubject");
			this.txtData 		= $("txtData");
			this.txtEMail 		= $("txtEMail");
			this.txtCaptcha 	= $("txtCaptcha");
			this.btnSend 		= $("btnSend");
			this.txtPleaseWait 	= $("txtPleaseWait");
			this.txtError 		= $("txtError");
			this.btnSend.observe("click", this.sendClick.bindAsEventListener(this));
			this.focus.bind(this).delay(0.2);
		}
	},
	
	focus: function() {
		this.txtSubject.focus();
	},
	
	validateEMail: function() {
		var r = /^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-]+\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$/;
		return (!r.test(this.txtEMail.getValue())) ?
			"Please enter a valid e-mail address.\n" : "";
	},
	
	validatePresence: function() {
		return ((this.txtSubject.getValue() != "") &&
					(this.txtData.getValue() != "") &&
					(this.txtEMail.getValue() != "") &&
					(this.txtCaptcha.getValue() != "")) ?
				"" : "All fields are required!";
	},
	
	validateData: function() {
		var msg = this.validateEMail() + this.validatePresence();
		if (msg != "")
		{
			alert(msg);
			return false;
		}
		return true;
	},
	
	getParameters: function() {
		return {
			s : this.txtSubject.getValue(),
			t : this.txtData.getValue(),
			e : this.txtEMail.getValue(),
			c : this.txtCaptcha.getValue()
		};
	},
	
	sendClick: function(e) {
		if (this.validateData())
		{
			this.btnSend.disable();
			this.txtError.hide();
			this.txtPleaseWait.show();
			new Ajax.Request("ajax/contact.php",
					{
						method 		: "get",
						parameters	: this.getParameters(),
						onComplete	: this.requestCompleted.bind(this)
					});
		}
	}, 
	
	requestCompleted: function(transport) {
		this.btnSend.enable();
		var json = transport.responseText.evalJSON();
		if (json.status != 0)
		{
			this.txtPleaseWait.hide();
			this.txtError.show();
		} else
		{
			alert("Contact request has been processing...");
			top.location = "home.php";
		}
	}
});
